var OpenFileDialog  = function() {

};

var openFileDialog = new OpenFileDialog();

openFileDialog.show = function(options) {
	var config = {
		filter: '',
		onInit: function(e) { },
		onFileNameSelected: function(file) { }
	};

	$.extend(config, options);

	var filter = config.filter;

	var dialogInputId = 'open-file-dialog-input';
	var browserElementHtml = '<input type="file" name="' + dialogInputId + '" ' + (filter.trim() != ''? 'accept="' + filter + '"' : '')  + ' style="display: none;">';

	var browserElement = $('input[name="' + dialogInputId + '"]');
	if (browserElement.length <= 0) {
		$('body').append(browserElementHtml);
		browserElement = $('input[name="' + dialogInputId + '"]');
	}

	var e = { cancel: false };
	config.onInit(e);
	cancel = e.cancel;
	
	if (cancel) return;

	browserElement.unbind('change');
	browserElement.change(function() {
		var file = browserElement[0].files[0];
		config.onFileNameSelected(file);
	});

	browserElement.trigger('click');
};